package grammar;

import java.util.Optional;

public class OptionalSample {
    public class InnerOptionalCls {
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

    public class OptionalClass {
        public InnerOptionalCls getInnerObject() {
            return innerObject;
        }

        public void setInnerObject(InnerOptionalCls innerObject) {
            this.innerObject = innerObject;
        }

        public InnerOptionalCls innerObject;
    }

    private Optional<OptionalClass> getOptionalObj() {
        OptionalClass optionalClass = new OptionalClass();
        InnerOptionalCls innerObj = new InnerOptionalCls();
        innerObj.setName("DJI");
        optionalClass.setInnerObject(innerObj);
        return Optional.of(optionalClass);
    }

    public void test() {
        String optionalName = getOptionalObj().map(OptionalClass::getInnerObject)
                .map(InnerOptionalCls::getName).orElse("未能找到");
        System.out.println(optionalName);
    }
}
